﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using WinDocker.Common.Global;

namespace WinDocker.Service
{
    public partial class WinDockerService : ServiceBase
    {
        public WinDockerService()
        {
            try
            {
                LogWrite.PathHead = AppDomain.CurrentDomain.BaseDirectory;
            }
            catch { }
            InitializeComponent();
        }

        /// <summary>
        /// 服务是否停止了
        /// </summary>
        private bool IsStop = false;

        protected override void OnStart(string[] args)
        {
            try
            {
                LogWrite.PathHead = AppDomain.CurrentDomain.BaseDirectory;
                string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
                this.IsStop = false;
                this.KnDProcessStart(baseDirectory);
            }
            catch (Exception ex)
            {
                LogWrite.TxtExport_DateNow("LogErr.txt", $"WinDockerService OnStart Err:{ex.Message}");
            }
        }

        protected override void OnStop()
        {
            try
            {
                this.IsStop = true;
                this.ProcessEnd();
            }
            catch (Exception ex)
            {
                LogWrite.TxtExport_DateNow("LogErr.txt", $"WinDockerService OnStop Err:{ex.Message}");
            }
        }

        /// <summary>
        /// KnD进程(基于文件夹方式的一键部署模块)
        /// </summary>
        private Process KnDProcess = null;
        /// <summary>
        /// 启动 KnD进程(基于文件夹方式的一键部署模块)
        /// </summary>
        /// <param name="baseDirectory"></param>
        private void KnDProcessStart(string baseDirectory)
        {
            try
            {
                if (this.KnDProcess != null)
                {
                    return;
                }
                string exeFilePath = baseDirectory + "Deploy\\WinDocker.Run.exe";
                if (!File.Exists(exeFilePath))
                {
                    LogWrite.TxtExport_DateNow("LogTrace.txt", $"\r\nWinDocker.Run.exe程序文件不存在!\r\n{exeFilePath}\r\n");
                    return;
                }
                string exeWorkingDirectory = baseDirectory + "Deploy";
                ProcessStartInfo info = new ProcessStartInfo();
                info.WorkingDirectory = exeWorkingDirectory;
                info.FileName = exeFilePath;
                //info.Arguments = "Start";
                this.KnDProcess = Process.Start(info);
                this.KnDProcess.EnableRaisingEvents = true;
                this.KnDProcess.Exited += ((s, e) =>
                {
                    try
                    {
                        if (this.IsStop)
                        {
                            return;
                        }
                        this.KnDProcess = null;
                        this.KnDProcessStart(baseDirectory);
                    }
                    catch (Exception ex)
                    {
                        LogWrite.TxtExport_DateNow("LogErr.txt", "WinDockerService KnDProcessStart 0Err:" + ex.ToString());
                    }
                });
                LogWrite.TxtExport_DateNow("LogTrace.txt", "\r\n启动了WinDocker.Run.exe程序进程!\r\n");
            }
            catch (Exception ex)
            {
                LogWrite.TxtExport_DateNow("LogErr.txt", "WinDockerService KnDProcessStart Err:" + ex.ToString());
            }
        }

        /// <summary>
        /// 程序进程停止处理.
        /// </summary>
        private void ProcessEnd()
        {
            try
            {
                try
                {
                    if (this.KnDProcess != null)
                    {
                        this.KnDProcess.Kill();
                        this.KnDProcess = null;
                        LogWrite.TxtExport_DateNow("LogTrace.txt", "\r\n结束了KnD程序进程!\r\n");
                    }
                }
                catch (Exception ex)
                {
                    LogWrite.TxtExport_DateNow("LogErr.txt", "WinDockerService ProcessEnd 5Err:" + ex.ToString());
                }
            }
            catch (Exception ex)
            {
                LogWrite.TxtExport_DateNow("LogErr.txt", "WinDockerService ProcessEnd Err:" + ex.Message);
            }
        }

    }
}
